package com.skp.shaphan.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DatabasePathSelector extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8010872051372907337L;
	private JTextField pathTextbox;
	private JButton selectPathButton;

	public DatabasePathSelector() {
		this.setLayout(new GridBagLayout());
		pathTextbox = new JTextField();
		pathTextbox.setPreferredSize(new Dimension(350, pathTextbox.getPreferredSize().height));
		this.add(pathTextbox, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		selectPathButton = new JButton("...");
		selectPathButton.setPreferredSize(new Dimension(selectPathButton.getPreferredSize().width, pathTextbox.getPreferredSize().height));
		selectPathButton.addActionListener(this);
		this.add(selectPathButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
	}

	public String getPath() {
		return pathTextbox.getText();
	}
	
	public void setPath(String newPath) {
		pathTextbox.setText(newPath);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result = fc.showOpenDialog(this);
		if(result == JFileChooser.APPROVE_OPTION) {
			pathTextbox.setText(fc.getSelectedFile().getAbsolutePath());
		}
	}
}
